<?php
class News_Model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function get_news($options = array())
    {
        $this->db->select(' ' . NEWS . '.id,
                            ' . NEWS . '.title,
                            ' . NEWS . '.summary,
                            ' . NEWS . '.content,
                            ' . NEWS . '.created_date,
                            ' . NEWS . '.edited_date,
                            ' . NEWS . '.viewed,
                            ' . NEWS . '.cat_id,
                            ' . NEWS . '.thumbnail,
                            ' . NEWS . '.cat_id as categories_id,
                            ' . NEWS . '.meta_title,
                            ' . NEWS . '.meta_keywords,
                            ' . NEWS . '.meta_description,
                            ' . NEWS . '.tags,
                            ' . NEWS . '.lang,
                            ' . NEWS . '.status,
                            ' . NEWS . '.show_homepage,
                            ' . NEWS_CATEGORIES . '.meta_keywords cat_meta_keywords,
                            ' . NEWS_CATEGORIES . '.parent_id,
                            ' . NEWS_CATEGORIES . '.category,
                            ' . NEWS_CATEGORIES . '.summarys,   
                            ' . NEWS_CATEGORIES . '.page_layout,
                            ' . NEWS_CATEGORIES . '.detail_layout,
                            ' . NEWS_CATEGORIES . '.item_layout,
                        ');
        

        $this->db->join(NEWS_CATEGORIES, NEWS_CATEGORIES . '.id = ' . NEWS . '.cat_id', 'left');

        // lọc ngôn ngữ
        if(isset($options['lang']))
        {
            $this->db->where(NEWS . '.lang', $options['lang']);
        }
        
        // chi lay nhung news moi nhat
        if (isset($options['flag']) && $options['flag']==LATEST_NEWS)
        {
            $this->db->where(NEWS . '.created_date >\' ' . $options['created_date'] . '\'');
            unset($options['id']); // loai bo id
        }

        if (isset($options['id']) && $options['id'] != '')
            if (isset($options['flag']) && $options['flag']==OLDER_NEWS)
                $this->db->where(NEWS . '.created_date <\' ' . $options['created_date'] . '\'');
            else
                $this->db->where(NEWS . '.id', $options['id']);

        // neu chon parent category thi se hien tat
        if (isset($options['cat_id']) && $options['cat_id'] != DEFAULT_COMBO_VALUE)
            $this->db->where('cat_id = ' . $options['cat_id']);
        if (isset($options['cats_id']) && $options['cats_id'] != DEFAULT_COMBO_VALUE)
            $this->db->where_in('cat_id', $options['cats_id']);
        
        // Tim kiem
        if (isset($options['title_search']) && $options['title_search'] != '')
        {
            $options['title_search'] = $this->db->escape_str($options['title_search']);
            $this->db->like('title', $options['title_search']);
        }
        
        if (isset($options['status']) && $options['status'] != DEFAULT_COMBO_VALUE)
        {
            $this->db->where('status', $options['status']);
        }
        
        if (isset($options['news_id']) && $options['news_id'] != '')
        {
            $this->db->where(NEWS . '.id', $options['news_id']);
        }
        
        if(isset($options['except_id']))
            $this->db->where(NEWS . '.id !=', $options['except_id']);
        
        if(isset($options['except_cate_id']))
            $this->db->where(NEWS_CATEGORIES . '.id !=', $options['except_cate_id']);
        
        if(!isset($options['is_admin']))
            $this->db->where('status', ACTIVE_NEWS);
        
        if(isset($options['show_homepage']))
            $this->db->where('show_homepage', SHOW_HOMEPAGE_NEWS);
        
        // Loc theo trang
        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);
        else if (isset($options['limit']))
            $this->db->limit($options['limit']);

        if(isset($options['order_by']))
            $this->db->order_by(NEWS . '.' . $options['order_by']);
        else
            $this->db->order_by(NEWS . '.created_date desc');
        
        $query = $this->db->get(NEWS);
        
        if (isset($options['first_row'])) return $query->row(0);
        if (isset($options['id']))
            if (!isset($options['flag']))
                return $query->row(0);
        return $query->result();
    }

    public function get_news_count($options = array())
    {
        return count($this->get_news($options));
    }

    function add_news($post_data = array())
    {
        $this->db->insert(NEWS, $post_data);
    }

    function update_news($post_data = array())
    {
        $this->db->where('id', $post_data['id']);
        $this->db->update(NEWS, $post_data);
    }

    function delete_news($id = 0)
    {
        $this->db->where('id', $id);
        $this->db->delete(NEWS);
    }

    public function update_view($id = 0)
    {
        $this->db->set('viewed', 'viewed + 1', FALSE);
        $this->db->where('id', $id);
        $this->db->update(NEWS);
    }
    
    function change_status($id=0, $status=0)
    {
        $this->db->where('id', $id);
        $this->db->update(NEWS, array('status'=> $status));
    }
    
    function change_show_homepage_status($id=0, $status=0)
    {
        $this->db->where('id', $id);
        $this->db->update(NEWS, array('show_homepage'=> $status));
    }
    
    public function up_top_new($new_id=0, $new_date=null){
        if ($new_date == null){
            $data['created_date'] = date('Y-m-d H:i:s');
        }
        else {
            $data['created_date'] = $new_date;
        }
        $this->db->where('id', $new_id);
        $this->db->update(NEWS, $data);
                
    }
    
    
}
?>